<?php

namespace App\Http\Controllers\Api\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\UserRepo;
use App\Models\UserCoordinate;
use App\Http\Requests\Api\ApiRequest;
use App\Services\RedisService;

class UserController extends Controller
{
    public function show(ApiRequest $request)
    {   
        $rs['status'] = false;
    	$id   = $request->user_id;
    	$data = UserRepo::find(['id'=>$id]);
    	if($data) {
    		$rs['status'] = true;
            $data->remember_token = '';
    		$rs['data']   = $data;
    	}
    	return response()->json($rs);    	
    }

    public function update(ApiRequest $request)
    {
        $id   = $request->user_id;
        $data = $request->pure();
        $data = UserRepo::update(['id'=>$id],$data);
        if($data) {
            $rs['status'] = true;
           // $rs['data'] = $data;
        }
        return response()->json($rs);       
    }

    public function coordInfo(ApiRequest $request)
    {
        $rs['status'] = false;
        $user_id      = $request->user_id;
        $flag = UserCoordinate::where(['user_id'=>$user_id])->first();
        if($flag) {
            $rs['status'] = true;
            $rs['data']   = $flag;
            return response()->json($rs);
        }
        return response()->json($rs);
    }

    public function coordSave(ApiRequest $request)
    {
        $rs['status'] = true;
        $data = $request->pure();
        $data['user_id'] = $request->user_id;
        $flag = UserCoordinate::updateOrCreate(['user_id'=>$data['user_id']],$data);
        if($flag) {
            $coord = explode(',', $data['coord']);
            RedisService::geoaAddUser($coord[0],$coord[1],$data['user_id']);
            $rs['data'] = $flag;
            return response()->json($rs);
        }
    }


}
